package com.example.orderfood2.Repository;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.widget.Toast;

import com.example.orderfood2.entity.TimeTable;

import java.util.ArrayList;
import java.util.List;

public class TimeTableRepository extends BaseRepository{
    public TimeTableRepository(Context context) {
        super(context);
    }
////    添加学生课程
    public long addTimeTable(TimeTable timeTable){
        open();
        long result=0;
        try {
            // 先检查是否已存在该学生的课程
            if (isTimeTableExists(timeTable.getStudentId())) {
                // 如果存在，先删除
                deleteTimeTableByStudentId(timeTable.getStudentId());
            }
            ContentValues values = createTimeTableContentValues(timeTable);
            result = database.insert("time_table", null, values);

        } catch (Exception e) {
            e.printStackTrace();
        }
        close();
        return result;
    }
//    根据学生id查询课表
    public TimeTable queryTimeTableByStudentId(Integer studentId) {
        open();
        TimeTable timeTable=null;
        Cursor cursor = database.rawQuery("SELECT * FROM time_table WHERE student_id=?", new String[]{String.valueOf(studentId)});
        if (cursor.moveToFirst()) {
            timeTable = new TimeTable(
                    cursor.getInt(cursor.getColumnIndexOrThrow("id")),
                    cursor.getInt(cursor.getColumnIndexOrThrow("student_id")),
                    cursor.getString(cursor.getColumnIndexOrThrow("first_class")),
                    cursor.getString(cursor.getColumnIndexOrThrow("second_class")),
                    cursor.getString(cursor.getColumnIndexOrThrow("third_class")),
                    cursor.getString(cursor.getColumnIndexOrThrow("fourth_class")),
                    cursor.getString(cursor.getColumnIndexOrThrow("fifth_class"))
            );

        }

        cursor.close();
        close();
        return timeTable;

    }


    // 创建 TimeTable 的 ContentValues
    private ContentValues createTimeTableContentValues(TimeTable timeTable) {
        ContentValues values = new ContentValues();
        values.put("student_id", timeTable.getStudentId());
        values.put("first_class", timeTable.getFirstClass());
        values.put("second_class", timeTable.getSecondClass());
        values.put("third_class", timeTable.getThirdClass());
        values.put("fourth_class", timeTable.getFourthClass());
        values.put("fifth_class", timeTable.getFifthClass());
        return values;
    }
    // 检查是否存在该学生的课程
    private boolean isTimeTableExists(int studentId) {
        boolean exists = false;
        try {
            String query = "SELECT * FROM time_table WHERE student_id = ?";
            Cursor cursor = database.rawQuery(query, new String[]{String.valueOf(studentId)});

            exists = cursor.getCount() > 0;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return exists;
    }
    // 根据学生ID删除课程
    private boolean deleteTimeTableByStudentId(int studentId) {
        boolean success = false;
        try {
            int rowsAffected = database.delete("time_table", "student_id = ?",
                    new String[]{String.valueOf(studentId)});
            success = rowsAffected > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }
}
